package com.carleasoft.mps.cms.business.education.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carleasoft.mps.cms.business.article.constants.ExceptionCMSEnum;
import com.carleasoft.mps.cms.business.education.bean.entity.EducationCollectEntity;
import com.carleasoft.mps.cms.business.education.bean.entity.EducationBasicEntity;
import com.carleasoft.mps.cms.business.education.dao.EduCollectMapper;
import com.carleasoft.mps.cms.business.education.service.EduCollectService;
import com.carleasoft.mps.cms.business.education.service.EduDoorService;
import com.carleasoft.mps.core.exceptions.BusinessException;
import com.carleasoft.mps.core.user.ThreadUserContext;
import com.carleasoft.mps.core.user.UserCacheData;
import com.carleasoft.mps.core.utils.IBeanUtil;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: yangHao
 * @createDate:2023/5/18
 * @description:
 */
@Service
public class EduCollectServiceImpl extends ServiceImpl<EduCollectMapper, EducationCollectEntity> implements EduCollectService {
    @Autowired
    private EduDoorService eduDoorService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveCategory(EducationCollectEntity educationCollectEntity, HttpServletRequest request) {
        boolean isOk = false;
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        IBeanUtil.paramVerify(educationCollectEntity, "categoryName");
        educationCollectEntity.setCreatePersonCode(user.getUserCode());
        educationCollectEntity.setCreatePersonName(user.getUserName());
        educationCollectEntity.setCreateTime(LocalDateTime.now());
        if (StrUtil.isNotBlank(educationCollectEntity.getCategoryCode())) {
            EducationCollectEntity collectEntity = this.getOne(Wrappers.<EducationCollectEntity>lambdaQuery()
                    .eq(EducationCollectEntity::getCategoryCode, educationCollectEntity.getCategoryCode()));
            collectEntity.setCategoryName(educationCollectEntity.getCategoryName());
            collectEntity.setCreatePersonCode(user.getUserCode());
            collectEntity.setCreatePersonName(user.getUserName());
            collectEntity.setCreateTime(LocalDateTime.now());
            isOk = this.updateById(collectEntity);
        } else {
            educationCollectEntity.setCategoryCode(IdWorker.getIdStr());
            isOk = this.save(educationCollectEntity);
        }

        return isOk;
    }

    @Override
    public List<EducationCollectEntity> categoryList() {
        return this.list();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean check(EducationCollectEntity educationCollectEntity) {
        List<EducationCollectEntity> collectEntity = new ArrayList<>();
        if (StrUtil.isBlank(educationCollectEntity.getCategoryCode())) {
            collectEntity = this.list(Wrappers.<EducationCollectEntity>lambdaQuery()
                    .eq(EducationCollectEntity::getEducation, educationCollectEntity.getEducation())
                    .eq(EducationCollectEntity::getCategoryName, educationCollectEntity.getCategoryName()));
        } else {
            collectEntity = this.list(Wrappers.<EducationCollectEntity>lambdaQuery()
                    .eq(EducationCollectEntity::getEducation, educationCollectEntity.getEducation())
                    .eq(EducationCollectEntity::getCategoryName, educationCollectEntity.getCategoryName())
                    .ne(EducationCollectEntity::getCategoryCode, educationCollectEntity.getCategoryCode()));
        }

        if (CollUtil.isNotEmpty(collectEntity)) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteCategory(EducationCollectEntity educationCollectEntity) {
        int count = eduDoorService.count(Wrappers.<EducationBasicEntity>lambdaQuery()
                .eq(EducationBasicEntity::getRCategory, educationCollectEntity.getCategoryCode()));
        if (count>0){
            throw new BusinessException(ExceptionCMSEnum.EXITS_NO);
        }

        return this.remove(Wrappers.<EducationCollectEntity>lambdaQuery()
                .eq(EducationCollectEntity::getCategoryCode, educationCollectEntity.getCategoryCode()));
    }
}
